package org.javaboy.meeting.controller;

import org.javaboy.meeting.model.Employee;
import org.javaboy.meeting.service.ChangePasswordService;
import org.javaboy.meeting.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


@Controller
public class ChangePasswordController {

    @Autowired
    ChangePasswordService changePasswordService;

    /**
     * 修改密码
     * @param employeeid
     * @return
     */
    @RequestMapping("/changepassword")
    public String changepassword(Integer employeeid){
        return "changepassword";
    }

    @RequestMapping("/sureChangePassword")
    public String sureChangePassword(String originpassword, String newpassword, HttpSession session, Model model){
        //获得当前登录用户
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        if (!Md5.JM(currentuser.getPassword()).equals(originpassword)){
            model.addAttribute("error", "原始密码错误");
            return "forward:/changepassword";
        }else{
            changePasswordService.updatePassword(newpassword,currentuser.getEmployeeid());
            return "forward:/";
        }
    }
}
